package com.zhongkai.service.book;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zhongkai.dao.book.TDjCcdjxxDAO;
import com.zhongkai.dao.book.TDjCcdjxxLsDAO;
import com.zhongkai.dao.book.TDjCcywclDAO;
import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.book.TDjCcdjxxLs;
import com.zhongkai.model.book.TDjCcywcl;
import com.zhongkai.model.config.User;
import com.zhongkai.service.BaseService;
@Component
@Transactional
public class MoveoutService extends BaseService{
	private TDjCcdjxxDAO tDjCcdjxxDAO;
	private TDjCcywclDAO tDjCcywclDAO;
	private TDjCcdjxxLsDAO tDjCcdjxxLsDAO;
	@Resource
	public void settDjCcdjxxDAO(TDjCcdjxxDAO tDjCcdjxxDAO) {
		this.tDjCcdjxxDAO = tDjCcdjxxDAO;
	}
	@Resource
	public void settDjCcywclDAO(TDjCcywclDAO tDjCcywclDAO) {
		this.tDjCcywclDAO = tDjCcywclDAO;
	}
	@Resource
	public void settDjCcdjxxLsDAO(TDjCcdjxxLsDAO tDjCcdjxxLsDAO) {
		this.tDjCcdjxxLsDAO = tDjCcdjxxLsDAO;
	}
	public void save(TDjCcdjxx djCcdjxx) throws Exception{
		tDjCcdjxxDAO.insert(djCcdjxx);
	}
	
	public List<TDjCcdjxx>findByHql(String hql,Object[] values){
		return tDjCcdjxxDAO.findByHql(hql, values);
	}
	
	public void savemoveout_xx(TDjCcdjxx tDjCcdjxx,TDjCcywcl tDjCcywcl,HttpServletRequest request) throws Exception{
		
		
		TDjCcdjxx tDjCcdjxxOld=(TDjCcdjxx)tDjCcdjxxDAO.selectById(TDjCcdjxx.class, tDjCcdjxx.getCcdjh());
		
		TDjCcdjxxLs tDjCcdjxxLs=new TDjCcdjxxLs();
		BeanUtils.copyProperties(tDjCcdjxxOld, tDjCcdjxxLs);
		User user=(User)request.getSession().getAttribute("user");
		Date date=new Date();
		//设置迁出状态02
		tDjCcdjxxOld.setDjztDm("02");
		tDjCcdjxx.setXgrDm(user.getUserIdentifier());
		tDjCcdjxx.setXgRq(date);
		//更新登记表记录
		tDjCcdjxxDAO.update(tDjCcdjxxOld);
		
		//业务处理表登记号参照了登记信息表
		tDjCcywcl.setCcdjh(tDjCcdjxx.getCcdjh());
		tDjCcywcl.setCzrDm(user.getUserIdentifier());
		tDjCcywcl.setCzRq(date);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
		Date sbnyDate=sdf.parse(tDjCcdjxx.getSbqsny());
		tDjCcywcl.setSbRq(sbnyDate);
		//设置业务处理代码迁出03
		tDjCcywcl.setYwlxDm("03");
		//保存业务处理表
		tDjCcywclDAO.insert(tDjCcywcl);
		
		
		//设置业务处理号，参考了业务处理表
		tDjCcdjxxLs.setCcdjlsXh(tDjCcywcl.getYwclXh());
		//保存历史登记信息表
		tDjCcdjxxLsDAO.insert(tDjCcdjxxLs);
	}
}
